package com.dic.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dic.mall.product.entity.AttrAttrgroupRelationEntity;
import com.dic.mall.product.service.AttrAttrgroupRelationService;
import com.dic.mall.product.service.AttrService;
import com.dic.mall.product.service.CategoryService;
import com.dic.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dic.mall.product.entity.AttrGroupEntity;
import com.dic.mall.product.service.AttrGroupService;
import com.dic.common.utils.PageUtils;
import com.dic.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 属性分组
 *
 * @author liliang
 * @email LeeLiang2016@163.com
 * @date 2020-07-06 17:45:28
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId")Long catelogId){
        PageUtils page =  attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
       List list =  attrService.getAttrRelation(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 查询三级分类下的属性分组和属性
     * @param catelogId
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }


    /**
     * 获取当前分组没有关联的属性
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.getNoAttrRelation(params,attrGroupId);
        return R.ok().put("page", page);
    }



    @Autowired
    private CategoryService categoryService;
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[]categoryPath = categoryService.getCategoryPath(catelogId);
        attrGroup.setCatelogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 保存
     */
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrAttrgroupRelationEntity[] entities){
        attrAttrgroupRelationService.saveBatch(Arrays.asList(entities));
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 删除分组和属性的关联
     */
    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody AttrAttrgroupRelationEntity[] entities){
        attrService.deleteRelation(entities);
        return R.ok();
    }
}
